package com.baiye.template.service.impl;

import com.baiye.template.dao.RoleDao;
import com.baiye.template.pojo.Role;
import com.baiye.template.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/7 16:48
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public int deleteById(int id) {
        return roleDao.deleteById(id);
    }

    @Override
    public int addRole(Role role) {
        return roleDao.add(role);
    }

    @Override
    public int addRoleToUser(int userId, int roleId) {
        return roleDao.addRoleToUser(userId, roleId);
    }

    @Override
    public int deleteRoleToUser(int userId, int roleId) {
        return roleDao.deleteRoleToUser(userId, roleId);
    }

    @Override
    public List<Role> getRole(Role role, Integer pageIndex, Integer pageSize) {
        int start = (pageIndex-1)*pageSize;
        PageHelper.startPage(pageIndex,pageSize);
        return roleDao.getRole(role, start,pageSize);
    }

    @Override
    public List<Role> getRoleByUserId(Integer userId) {
        List<Role> roleExist = roleDao.getRoleByUserIdExist(userId);
        for(Role r:roleExist){
            r.setOwned(true);
        }
        roleExist.addAll(roleDao.getRoleByUserIdNotExist(userId));
        return roleExist;
    }

    @Override
    public int updateRole(Role role) {
        return roleDao.updateRole(role);
    }
}
